const BASE_URL = 'https://application.huhuiyu.top'
const TOKEN_KEY = BASE_URL + '_token'

const tokeninfo = {
	save: (data) => {
		if (data && data.token) {
			uni.setStorageSync(TOKEN_KEY, data.token)
		}
	},
	load: () => {
		let token = uni.getStorageSync(TOKEN_KEY)
		return token ? token : ""
	},
	remove: () => {
		uni.removeStorageSync(TOKEN_KEY)
	}
}

const api = {
	send: (url, param, cb, method) => {
		uni.request({
			url: BASE_URL + url,
			method: method,
			data: param,
			header: {
				Authorization: tokeninfo.load()
			},
			success: (resp) => {
				tokeninfo.save(resp.data)
				cb(resp.data)
			},
			fail: (err) => {
				console.log(err)
				cb({
					success: false,
					code: 500,
					message: '程序出bug了，赶紧修复'
				})
			},
		})
	},
	get: (url, param, cb) => {
		api.send(url, param, cb, "GET")
	},
	post: (url, param, cb) => {
		api.send(url, param, cb, "POST")
	},
	put: (url, param, cb) => {
		api.send(url, param, cb, "PUT")
	},
	patch: (url, param, cb) => {
		api.send(url, param, cb, "PATCH")
	},
	delete: (url, param, cb) => {
		api.send(url, param, cb, "DELETE")
	}
}

const tools = {
	apiBase: BASE_URL,
	userlogo: (uid) => {
		return `${BASE_URL}/user/logo/${uid}`
	},

	/**
	 * 格式化日期
	 * @param value 时间对象，可以时Date或者时间戳
	 * @param format 格式化字符串
	 * @returns 格式化日期之后的结果
	 */
	formatDate: (value, format = 'yyyy-MM-dd hh:mm:ss') => {
		try {
			let time = new Date();
			if (typeof value === 'number') {
				time.setTime(parseInt(value.toString()));
			} else if (value instanceof Date) {
				time = value;
			}
			let year = time.getFullYear() + '';
			let month = time.getMonth() + 1 + '';
			let day = time.getDate() + '';
			let hour = time.getHours() + '';
			let minute = time.getMinutes() + '';
			let second = time.getSeconds() + '';
			let ms = time.getMilliseconds() + '';
			month = month.padStart(2, '0');
			day = day.padStart(2, '0');
			hour = hour.padStart(2, '0');
			minute = minute.padStart(2, '0');
			second = second.padStart(2, '0');
			ms = ms.padStart(3, '0');

			format = format.replace(/yyyy/g, year);
			format = format.replace(/MM/g, month);
			format = format.replace(/dd/g, day);
			format = format.replace(/hh/g, hour);
			format = format.replace(/mm/g, minute);
			format = format.replace(/ss/g, second);
			format = format.replace(/ms/g, ms);
			return format;
		} catch (ex) {
			console.error(ex);
			return '';
		}
	},

	/**
	 * 合并多个json对象
	 * @param jsons 要合并的多个json
	 * @returns 合并后的json对象
	 */
	concatJson: (...jsons) => {
		let json = {};
		for (let index = 0; index < jsons.length; index++) {
			let element = jsons[index];
			for (let key in element) {
				json[key] = element[key];
			}
		}
		return json;
	},
	replaceInfoImg(info) {
		if (!info) {
			return info
		}
		// 正则表达式匹配<img开始的所有标签，但不包括>
		const regex = /<img([^>]*)>/g
		let style = 'display: block;width:90%;height: auto;margin: 10px auto;'

		let replacedString = info.replace(regex, function(match, p1) {
			console.log('图片正则替换', match)
			// p1是匹配到的第一个分组，即<img标签的所有属性
			// 在属性前插入style属性
			return `<img style="${style}"${p1 ? ' ' + p1 : ''}>`
		})
		replacedString = replacedString.replace(/\r\n/g, '<br>')
		replacedString = replacedString.replace(/\n/g, '<br>')
		replacedString = replacedString.replace(/\r/g, '<br>')
		return replacedString
	}

}

export {
	api as
	default, api as api, tools as tools, BASE_URL as BASE_URL
}